
import conf
from curses import *
from songutils import songrepr
import time
from draw import clrline
from playmgr import playcount_to_string

def _clip0(x):
    if x < 0:
        return 0
    return x

class SearchBox:
    def __init__(self, y, searchcb):
        self.y = y
        self.x = _clip0(conf.SCREEN_W/2 - 35)
        self.text = ""
        self.list = []
        self.listpos = 0
        self.scrollpos = 0
        self.height = 10
        self.searchcb = searchcb
        #self.lastkeypress = time.time()
        self.text_dirty = True
        self.results_due_for_redraw = True

    def onkeypress(self, key):
        destroy = False
        #thistime = time.time()
        #time_since_last_keypress = thistime - self.lastkeypress
        if key == conf.KEY_UP:
            self.listpos = _clip0(self.listpos - 1)
            if self.scrollpos > self.listpos:
                self.scrollpos -= 1
                assert self.scrollpos >= 0
            self.results_due_for_redraw = True
            self.redraw()
        elif key == conf.KEY_DOWN:
            if (self.listpos + 1) < len(self.list):
                self.listpos += 1
                if self.listpos+1 > (self.scrollpos + self.height):
                    self.scrollpos += 1
            self.results_due_for_redraw = True
            self.redraw()
        elif key == conf.KEY_PAGEUP:
            self.listpos = _clip0(self.listpos - self.height)
            if self.scrollpos > self.listpos:
                self.scrollpos = _clip0(self.scrollpos - self.height)
                assert self.scrollpos >= 0
            self.results_due_for_redraw = True
            self.redraw()
        elif key == conf.KEY_PAGEDOWN:
            self.scrollpos += self.height - (self.listpos - self.scrollpos)
            if self.scrollpos > (len(self.list)-1):
                self.scrollpos = len(self.list)-1
            self.listpos += self.height
            if self.listpos > (len(self.list)-1):
                self.listpos = len(self.list)-1
            self.results_due_for_redraw = True
            self.redraw()
        elif key == conf.KEY_ENTER:
            if self.list:
                self.onselectnext(self.list[self.listpos])
                destroy = True
        elif key == ord('!'):
            if self.list:
                self.onselectnow(self.list[self.listpos])
                destroy = True
        elif key == ord('.'):
            if self.list:
                ahead = self.onselectlater(self.list[self.listpos])
                conf.screen.addstr(self.y+1, self.x, "Added %s songs in advance" % ahead, A_NORMAL)
                destroy = True
        elif ((ord('a') <= key <= ord('z')) or
              (ord('A') <= key <= ord('Z')) or
              (ord('0') <= key <= ord('9')) or
              key in (ord(' '), ord('-'))):
            c = chr(key)
            self.text += c
            self.text_dirty = True
            #if time_since_last_keypress > conf.ISEARCH_DELAY:
            self.invalidate_search()
            self.scrollpos = 0
            self.listpos = 0
        elif key == conf.KEY_BACKSPACE:
            self.text = self.text[:-1]
            self.text_dirty = True
            #if time_since_last_keypress > conf.ISEARCH_DELAY:
            self.invalidate_search()
            self.scrollpos = 0
            self.listpos = 0
        #self.lastkeypress = thistime
        return destroy

    def tick(self):
        pass
        #if self.text_dirty and (time.time() - self.lastkeypress) > conf.ISEARCH_DELAY:
        #    self.invalidate_search()

    def invalidate_search(self):
        self.list = self.searchcb(self.text)
        self.results_due_for_redraw = True
        self.text_dirty = False
        self.redraw()

    # replace this
    def onselect(self, item):
        pass

    def redraw(self):
        clrline(self.y)
        conf.screen.addstr(self.y, self.x-2, conf.M_SEARCH)
        conf.screen.addstr(self.y, self.x-2+len(conf.M_SEARCH), self.text, A_NORMAL)
        for i in range(self.height+1):
            clrline(self.y+2+i)
        for i, song in enumerate(self.list[self.scrollpos:self.scrollpos+self.height]):
            text = songrepr(song)
            if len(text) > 60:
                text = text[:57] + "..."
            pos = i + self.scrollpos
            conf.screen.addstr(self.y+2+i, self.x, text, A_BOLD if pos == self.listpos else A_NORMAL)
            conf.screen.addstr(self.y+2+i, self.x+61, playcount_to_string(song.playcount), A_NORMAL)

    def blank(self):
        clrline(self.y)
        for i in range(self.height):
            clrline(self.y+2+i)
